import java.util.Arrays;
import java.util.Comparator;

class Person implements Comparable<Person>{
    public String name;
    public int age;
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "[" + this.name + "," + this.age + "]";
    }

    @Override
    public int compareTo(Person o) {
        return this.age - o.age;
    }
}
class NameComparator implements Comparator<Person>{
    @Override
    public int compare(Person o1, Person o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test2 {
    public static void main(String[] args) {
        Person people1 = new Person("zhangsan",10);
        Person people2 = new Person("lisi",5);
        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(people1,people2));
    }
    public static void bubble(Comparable[] comparable){
        for (int i = 0; i < comparable.length - 1; i++) {
            for (int j = 0; j < comparable.length - 1 - i; j++) {
                if(comparable[j].compareTo(comparable[j+1]) > 0){
                    Comparable tmp = comparable[j];
                    comparable[j] = comparable[j+1];
                    comparable[j+1] = tmp;
                }
            }
        }
    }
    public static void main1(String[] args) {
        Person[] people = new Person[3];
        people[0] = new Person("zhangsan",10);
        people[1] = new Person("lisi",5);
        people[2] = new Person("wangwu",9);
        System.out.println("排序前："+Arrays.toString(people));
        bubble(people);
        System.out.println("排序后："+Arrays.toString(people));
    }
}
